package com.peas.platform.common.core.utils;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.common.core.annotation.Aop;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 日志输出工具类
 *
 * @author caizhh
 */
@Slf4j
@Aop(enabled = false)
public class LogUtil {

    /**
     * 调用api开始前，日志输出
     *
     * @param desc  描述
     * @param url   调用地址
     * @param param 参数
     */
    public static void callApiLogStart(String desc, String url, String param) {
        log.info("接口调用: " + desc + ", url = " + url + ", param = " + param);
    }

    /**
     * 调用api结束后，日志输出
     *
     * @param desc   描述
     * @param result 调用返回结果
     */
    public static void callApiLogEnd(String desc, String result) {
        log.info("接口调用: " + desc + ", result = " + result);
    }

    /**
     * 调用api未返回正确数据，日志输出
     *
     * @param desc   描述
     * @param result 调用返回结果
     */
    public static void callApiLogFail(String desc, String result) {
        log.error("接口调用: " + desc + ", result = " + result);
    }

    /**
     * 调用api正常返回，接口结果有异常信息
     *
     * @param desc   String
     * @param result String
     */
    public static void callApiLogWarn(String desc, String result) {
        log.warn("接口调用: " + desc + ", result = " + result);
    }

    public static void log(Class<?> clazz, String method, String str) {
        log(clazz, method, str, 0L);
    }

    public static void log(Class<?> clazz, String method, String str, Long ms) {
        log(clazz, method, str, ms, null);
    }

    public static void log(Class<?> clazz, String method, String str, Long ms, String prefix) {
        if (ms > 5000)
            log("error", clazz, method, str, ms, prefix);
        else if (ms > 3000)
            log("warn", clazz, method, str, ms, prefix);
        else if (ms > 5)
            log("info", clazz, method, str, ms, prefix);
        else if (ms > 1)
            log("debug", clazz, method, str, ms, prefix);
        else
            log("trace", clazz, method, str, ms, prefix);
    }

    public static void log(String level, Class<?> clazz, String method, String str, Long ms) {
        log(level, clazz, method, str, ms, null);
    }

    public static void log(String level, Class<?> clazz, String method, String str, Long ms, String prefix) {
        log(level, clazz, method, str, ms, TimeUnit.MILLISECONDS, prefix);
    }

    public static void log(String level, Class<?> clazz, String method, String str, Long time, TimeUnit timeUnit, String prefix) {
        String cost = "";
        if (time != null) {
            cost = ",cost:" + timeUnit.toMillis(time) + "ms";
        }
        if (level == null) {
            level = "info";
        }
        prefix = StrUtil.isNotBlank(prefix) ? "【" + prefix + "】" : "";
        String c = prefix + clazz.getSimpleName() + "." + method + "," + str + cost;
        switch (level.toLowerCase()) {
            case "trace":
                log.trace(c);
                break;
            case "debug":
                log.debug(c);
                break;
            case "info":
                log.info(c);
                break;
            case "warn":
                log.warn(c);
                break;
            case "error":
                log.error(c);
                break;
            default:
                log.debug("parameter level is error");
                log.info(c);
        }
    }

}
